package com.atguigu.eduservice.controller.front;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.commonutils.orderVo.CourseWebVoOrder;
import com.atguigu.commonutils.orderVo.UcenterMemberOrder;
import com.atguigu.eduservice.client.OrderClient;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.frontvo.CourseFrontVo;
import com.atguigu.eduservice.entity.frontvo.CourseWebVo;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/eduservice/coursefront")
@Api(tags = "前台课程管理")
//@CrossOrigin
public class CourseFrontController {

    @Autowired
    private EduCourseService eduCourseService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private OrderClient orderClient;

    @ApiOperation("前台分页条件查询课程列表")
    @GetMapping("getCourseList")
    public R getCourseList(@RequestParam long pageNum, @RequestParam long pageSize, CourseFrontVo courseFrontVo) {
        Page<EduCourse> coursePage = new Page<>(pageNum,pageSize);
        Map<String,Object> map = eduCourseService.getCourseFrontList(coursePage,courseFrontVo);
        return R.ok().data(map);
    }
    @ApiOperation("前台分课程")
    @GetMapping("getCourse/{courseId}")
    public R getCourse(@PathVariable String courseId, HttpServletRequest request) {
        // sql语句查询
        CourseWebVo courseWebVo = eduCourseService.getBaseCourseInfo(courseId);
        // 获取章节和小计
        List<ChapterVo> chapterVideoList = chapterService.findChapterVideoById(courseId);

        // orderClient
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        boolean isBuy = orderClient.isBuyCourse(courseId, memberId);

        return R.ok().data("courseWebVo",courseWebVo).data("chapterVideoList",chapterVideoList).data("isBuy",isBuy);
    }

    @ApiOperation("根据课程id查询课程信息")
    @GetMapping("getCourseOrder/{id}")
    public CourseWebVoOrder getCourseOrder(@PathVariable("id") String id) {
        CourseWebVo courseWebVo = eduCourseService.getBaseCourseInfo(id);
        CourseWebVoOrder courseWebVoOrder = new CourseWebVoOrder();
        BeanUtils.copyProperties(courseWebVo,courseWebVoOrder);
        return courseWebVoOrder;
    }



}
